﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProdeFrontEnd.Clases;
using ProdeFrontEnd.ServiceReferenceProde;
using System.Data;

namespace ProdeFrontEnd.Pags
{
    public partial class Forecast : System.Web.UI.Page
    {
        private ServiceProde serviceProde;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["User"] == null) //Se tiene que salir
            {
                Response.Redirect("NewUser.aspx");
            }
            serviceProde = ServiceProde.getInstanceServiceProde();
            if (!IsPostBack)
            {
                loadUserResult();
            }
        }

        private void loadUserResult()
        {
            Match[] matches = serviceProde.prodeClient.getMatchMissing((User)Session["User"]);
            
            DataTable dataTable = new DataTable();
            dataTable.Columns.AddRange(new DataColumn[9] {
                                new DataColumn("pathFlag1",typeof(string)),
                                new DataColumn("team1",typeof(string)),
                                new DataColumn("VTeam1",typeof(string)),
                                new DataColumn("vs", typeof(string)),
                                new DataColumn("VTeam2",typeof(string)),
                                new DataColumn("team2",typeof(string)),
                                new DataColumn("pathFlag2",typeof(string)),
                                new DataColumn("tournament",typeof(string)),
                                new DataColumn("idMatch",typeof(string))});
            
            foreach (Match match in matches)
            {
                Team team1 = match.team1;
                Team team2 = match.team2;
                Tournament tournament = match.tournamente;
                dataTable.Rows.Add(team1.pathFlag, team1.name, "", "-", "", team2.name, team2.pathFlag, tournament.name, match.id);
            }

            GridView1.DataSource = dataTable;
            GridView1.DataBind();

            if (dataTable.Rows.Count > 0)
            {
                Label1.Visible = false;
                Button1.Visible = true;
            }
            else
            {
                Label1.Visible = true;
                Button1.Visible = false;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in GridView1.Rows)
            {
                
                UserResult userResult = new UserResult();

                TextBox txtVTeam1 = ((TextBox)(row.FindControl("a")));
                TextBox txtVTeam2 = ((TextBox)(row.FindControl("b")));
                
                if (!txtVTeam1.Text.Trim().Equals("") && !txtVTeam2.Text.Trim().Equals(""))
                {
                    try
                    {
                        int VTeam1 = Convert.ToInt32(txtVTeam1.Text);
                        int VTeam2 = Convert.ToInt32(txtVTeam2.Text);
                        int idMatch = Convert.ToInt32(row.Cells[8].Text);

                        Match match = serviceProde.prodeClient.getMatchById(idMatch);
                        userResult.user = (User)Session["User"];
                        userResult.match = match;
                        userResult.VTeam1 = VTeam1;
                        userResult.VTeam2 = VTeam2;
                        userResult.points = 0;
                        serviceProde.prodeClient.saveUserresult(userResult);
                    }
                    catch (Exception ex)
                    { 
                    }
                }
            }
            loadUserResult();
        }
    }
}